package com.atech.ebayadmin.system.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import com.atech.ebayadmin.system.model.Role;
import com.atech.ebayadmin.system.model.User;
import com.atech.ebayadmin.system.dao.ISystemDAO;
import com.atech.ebayadmin.system.model.SystemConfig;
import com.atech.ebayadmin.common.util.BaseDAOHibernate;

public class SystemDAO extends BaseDAOHibernate implements ISystemDAO {
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SystemDAO.class);
	@Override
	protected Class getObjClass() {
		return SystemConfig.class;
	}
	public List<Role> listAllRole()throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("from Role t where 1=1 ");
		Query query = super.getSession().createQuery(sb.toString());
		return query.list();
	}
	public Role getRole(Integer roleId)throws Exception{
		return (Role)super.getHibernateTemplate().get(Role.class, roleId);
	}

	public List<SystemConfig> listSystemConfig()throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("from SystemConfig t where 1=1");
		Query query = super.getSession().createQuery(sb.toString());
		return query.list();
	}
    public boolean isHasPermission(Integer roleId,String url){
		StringBuilder sb = new StringBuilder();
		sb.append("select count(*) from RoleAndPerm rp , Permission p where rp.permId = p.permId ");
		sb.append(" and rp.roleId =").append(roleId);
		sb.append(" and p.visitUrl='").append(url).append("'");
		Query query = super.getSession().createQuery(sb.toString());
		Number count = (Number)query.uniqueResult();
		if(count.intValue() > 0){
			return true;
		}
		return false;
    }
    public List<String> listAllPermission()throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("select t.visitUrl from Permission t");
		Query query = super.getSession().createQuery(sb.toString());
		List<Object> queryList = query.list();
		List<String> urlList = new ArrayList<String>();
		if(queryList != null && queryList.size() >0){
			for(Object o : queryList){
				String url = (String)o;
				urlList.add(url);
			}
		}
		return urlList;
    }
	
}
